<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增轮播')" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-manage-add">
        <div class="form-group">
            <label class="col-sm-3 control-label">类型选择：</label>
            <div class="col-sm-8">
            <select name="reserve" id="reserve" class="form-control m-b">
               <option value="0">跳转店铺</option>
               <option value="1">跳转外网</option>
            </select>
            </div>
        </div>

        <div class="form-group">
            <label class="col-sm-3 control-label">上传图片：</label>
            <div class="col-sm-8">
                <div class="file-loading">
                    <input id="imgFile" name="imgFile" class="file" type="file"  data-theme="fas" accept=".jpg,.img,.png">
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">序号：</label>
            <div class="col-sm-8">
                <input name="status" id="status" class="form-control" type="number">
            </div>
        </div>
        <div class="form-group" id="dianPu">
            <label class="col-sm-3 control-label">店铺：</label>
            <input name="shopId" id="shopId"  type="text" hidden>
            <div class="col-sm-8">
                <input  id="shopName" class="form-control" type="text" onclick="selectShop()">
            </div>
        </div>
        <div class="form-group" id="waiWang" hidden>
            <label class="col-sm-3 control-label">外网地址：</label>
            <div class="col-sm-8">
                <input name="seatId" id="seatId" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <input name="comments" class="form-control" type="text">
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: bootstrap-fileinput-js" />
<th:block th:include="include :: datetimepicker-js" />
<script type="text/javascript">
    var prefix = ctx + "carousel/carousel"
    $("#form-manage-add").validate({
        onkeyup: false,
        rules:{
            status:{
                remote: {
                    url: prefix + "/checkStatusUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "status": function () {
                            return $.common.trim($("#status").val());
                        }
                    },
                    dataFilter: function (data, type) {
                        return $.validate.unique(data);
                    }
                }
            },
        },
        messages: {
            "status":{
                remote: "序号重复"
            }
        },
        focusCleanup: true
    });
    $("#imgFile").fileinput({
        uploadAsync:false,//默认异步上传
        showUpload:false,//是否显示上传按钮
    })
    $("#reserve").change(function () {
        $("#shopId").val('')
        $("#seatId").val('')
       var reserve= $('#reserve').val()
        if (reserve ==0){   //店铺
            $('#dianPu').show()
            $('#waiWang').hide()
        }else {
            $('#dianPu').hide()
            $('#waiWang').show()
        }
    })


    function submitHandler() {
        if ($.validate.form()) {
            if ($('#imgFile')[0].files[0] == null) {
                $.modal.alertWarning("请先选择文件路径");
                return false;
            }
            var reserve= $('#reserve').val()
            if (reserve==0){
               if (! $("#shopName").val()){
                   $.modal.alertWarning("店铺不能为空");
                   return false;
               }
            } else {
                if ( !$("#seatId").val()){
                    $.modal.alertWarning("外网链接不能为空");
                    return false;
                }
            }

            var formData = new FormData($("#form-manage-add")[0]);
            formData.append('imgFile', $('#imgFile')[0].files[0]);
            var loading;
            $.ajax({
                url: prefix + "/add",
                type: 'post',
                cache: false,
                data: formData,
                processData: false,
                contentType: false,
                dataType: "json",
                beforeSend:function(){
                    loading = layer.load(1, {
                        shade: [0.5,'#000'] //0.1透明度的背景
                    })

                },
                success: function(result) {
                    layer.close(loading);
                    $.operate.successCallback(result);
                }
            });
        }
    }
    
    function selectShop() {
        var url=prefix+"/selectShop";
        $.modal.open("选择店铺",url);
    }

</script>
</body>
</html>